// index.js
import {HomeListApi, SeckillApi,TeamApi} from "../../api/home"
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    homelist:[],
    page:1,
    size:10,
    isModalVisible: false,//自定义模态框
  },

  // 编写本地封装请求
  async getHomeListData(){
    wx.showLoading({
      title: '加载中',
    })
    let res=await HomeListApi({
      page:this.data.page,
      size:this.data.size,
      type:this.data.type,
    })
    this.setData({
      homelist:this.data.homelist.concat(res.data.data)
    },()=>{
      wx.hideLoading()//管理懒加载
    })
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  onScanCode(e){
    wx.scanCode({
      onlyFromCamera:true,
      success:(res)=>{
        console.log(res,'扫码函数')
      }
    })
  },
  onAuthSetting(e){
    wx.getSetting({
      success:(res)=>{
        res.authSetting={
          "scope.camera":true
        }
        console.log(res,'目前授权信息')
      }
    })
  },
  Login(e){
    wx.login({
      success:(res)=>{
        console.log(res,'登录授权')
      }
    })
  },
  showToast(){
    wx.showToast({
      title: '成功弹窗',
      duration:2000,
      icon:'loading',
      mask:true
    })
  },
  showModal(){
    wx.showModal({
      title: '对话框',
      content: '是否允许继续',
      complete: (res) => {
        if (res.cancel) {
          console.log('用户点了取消')
        }
        if (res.confirm) {
          console.log('用户点击了确认')
        }
      }
    })
  },
  showLoading(){
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function(){
      wx.hideLoading()
    },2000)
  },
  showActionSheet(){
    wx.showActionSheet({
      itemList: ['中国','俄罗斯','冰岛'],
      success:()=>{
        switch(res.tapIndex){
          case 0:
            console.log('点击了 中国')
            break;
          case 1:
            console.log('点击了 俄罗斯')
            break;
          case 2:
            console.log('点击了 冰岛')
            break;
        }
      },
    
    })
  },
// 自定义模态框函数开始
showCustomModal() {
  this.setData({
      isModalVisible: true
  });
},
handleCancel() {
  this.setData({
      isModalVisible: false
  });
  console.log('用户点击了取消');
},
handleConfirm() {
  this.setData({
      isModalVisible: false
  });
  console.log('用户点击了确定');
},
// 自定义模态框结束
// 获取微信 定位信息
getLocation(){
  wx.getLocation({
    type: 'gcj02',
    success (res) {
      const latitude = res.latitude
      const longitude = res.longitude
      const speed = res.speed
      const accuracy = res.accuracy
      console.log(latitude,longitude,speed,accuracy)
    }
   })
   wx.chooseLocation({
    success: (res) => {
      console.log(res);
     }
   })
},

})
